package com.wkhyapp.lm.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
//
//import com.fzh.sshop.utils.StringUtils;
//import com.maning.mndialoglibrary.MProgressDialog;

import com.maning.mndialoglibrary.MProgressDialog;
import com.wkhyapp.lm.utils.StringUtils;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

public abstract class BaseFragment extends Fragment {
    protected Context context;
    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeCreate();
        context = getActivity();
        initParams();
    }

    private void initParams() {
    }

    protected void beforeCreate() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        if (view == null) {
            view = inflater.inflate(getLayoutId(), null);
            ButterKnife.bind(this, view);
            initUI();
            initListener();
            initData();
        }
        return view;
    }


    /**
     * 页面初始化数据
     */
    protected abstract void initData();


    /**
     * 监听注册
     */
    protected abstract void initListener();


    /**
     * @return 布局的layoutid
     */
    public abstract int getLayoutId();


    /**
     * 适配UI
     */
    public abstract void initUI();

    /**
     * 获取当前时间
     **/
    protected String getTime() {
        return new SimpleDateFormat("MM-CategoryAdapter HH:mm", Locale.CHINA).format(new Date());
    }


    /**
     * 参数传递(注：该方法为了方便传参,参数类型有局限性,传递特殊类型时单独构建Intent)
     **/
    public void goTo(Class<?> goToCls, Object... values) {
        Intent intent = new Intent(getActivity(), goToCls);
        int i = 0;
        for (Object value : values) {
            Class<?> cls = value.getClass();
            String key = String.valueOf(i);
            if (cls == String.class) {
                intent.putExtra(key, (String) value);
            } else if (cls == int.class || cls == Integer.class) {
                intent.putExtra(key, (Integer) value);
            } else if (cls == long.class || cls == Long.class) {
                intent.putExtra(key, (Long) value);
            } else if (cls == boolean.class || cls == Boolean.class) {
                intent.putExtra(key, (Boolean) value);
            } else if (value instanceof Serializable) {
                intent.putExtra(key, (Serializable) value);
            } else if (cls == float.class || cls == Float.class) {
                intent.putExtra(key, (Float) value);
            } else if (cls == double.class || cls == Double.class) {
                intent.putExtra(key, (Double) value);
            } else if (cls == char.class || cls == Character.class) {
                intent.putExtra(key, (Character) value);
            } else if (cls == Bundle.class) {
                intent.putExtra(key, (Bundle) value);
            } else if (cls == long[].class) {
                intent.putExtra(key, (long[]) value);
            } else {
                throw new RuntimeException("不支持参数类型: " + cls.getSimpleName());
            }
            i++;
        }
        startActivity(intent);
    }


    @Override
    public void onDestroyView() {

        super.onDestroyView();
    }


//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        onUnsubscribe();
//
//    }



    private CompositeSubscription mCompositeSubscription;

    public void onUnsubscribe() {
        //取消注册，以避免内存泄露
        if (mCompositeSubscription != null) {
            mCompositeSubscription.unsubscribe();
        }
    }

    public void addSubscription(Subscription subscription) {
//        if (mCompositeSubscription == null) {
        mCompositeSubscription = new CompositeSubscription();
//        }
        mCompositeSubscription.add(subscription);
    }


    /**
     * 显示控件
     *
     * @param v
     */
    public void VISIBLE(View v) {
        v.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏控件
     *
     * @param v
     */
    public void GONE(View v) {
        v.setVisibility(View.GONE);
    }


    public void setfail(String str) {
        Toast.makeText(getActivity().getApplicationContext(), str, Toast.LENGTH_SHORT).show();
    }


    public void updateApp(){

    }


    public void goLogin() {

    }



    /**
     * 加载动画
     *
     * @param str
     */
    public void showLoad(String str) {
        if(StringUtils.isEmpty(str)){
            MProgressDialog.showProgress(context,"正在加载...");
        }else{
            MProgressDialog.showProgress(context,str);
        }

    }

    /**
     * 关闭加载动画
     */
    public void dismissLoad() {
        if( MProgressDialog.isShowing()){
            MProgressDialog.dismissProgress();
        }
    }


    /**
     * 获取控件信息
     */
    public String getViewValue(View view) {
        if (view instanceof EditText) {
            return ((EditText) view).getText().toString();
        } else if (view instanceof TextView) {
            return ((TextView) view).getText().toString();
        } else if (view instanceof CheckBox) {
            return ((CheckBox) view).getText().toString();
        } else if (view instanceof RadioButton) {
            return ((RadioButton) view).getText().toString();
        } else if (view instanceof Button) {
            return ((Button) view).getText().toString();
        }
        return null;
    }



}
